<?php

/**
 * case the default connect charset of mysql is latin1
 * sempr forget use the mysql_set_charset set as utf8
 * there is no problem with english but will case garbled with chinese
 * chinaese may in nick,source code ,description and so on...
 * 2009-5-14 18:10
 *
 * @author freefcw
 */
$dbhost     = 'localhost';
$dbuser     = 'root';
$dbpass     = 'hejun';
$dbname     = 'judge';

$db = mysql_connect($dbhost, $dbuser, $dbpass);

mysql_select_db($dbname);
echo mysql_client_encoding($db);

function do_reply()
{
    global $db;
    $result = mysql_query('select user_id, nick_old from users', $db);
    mysql_set_charset('utf8');

    while ($row = mysql_fetch_object($result)) {
        // $sql = "UPDATE reply SET content = '{$row->content}' user_id = '{$row->user_id}'where rid={$row->rid}";
        $sql = "UPDATE reply SET user_id = '{$row->user_id}' where rid={$row->rid}";

        mysql_query($sql);
    }
    echo 'reply done';
}


function do_problem()
{
    global $db;
    $result = mysql_query('select title, input, output, description, problem_id from problem', $db);
    mysql_set_charset('utf8');

    while ($row = mysql_fetch_object($result)) {

        $sql = "update problem set title = {$row->title} input = '".
         mysql_real_escape_string($row->input). 
         "', output = '". mysql_real_escape_string($row->output).
         "', description = '" . mysql_real_escape_string($row->description) . 
         "' where problem_id='$row->problem_id'";

        mysql_query($sql);
    }
    echo 'problem done';
}

function do_user()
{
    global $db;
    $result = mysql_query('SELECT * from users', $db);
    mysql_set_charset('utf8');

    while ($row = mysql_fetch_object($result)) {
        $sql = "update users set users.nick_old = '$row->nick_old' where user_id='$row->user_id'";

        mysql_query($sql);
    }
    echo 'reply done';
}


?>
